# spider/signals.py
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from .models import Movie, CrawlLog
import logging
logger = logging.getLogger(__name__)
@receiver(post_save, sender=Movie)
def movie_saved(sender, instance, created, **kwargs):
    """电影保存后的信号处理"""
    if created:
        logger.info(f"新电影已保存: {instance.title}")
    else:
        logger.info(f"电影已更新: {instance.title}")
@receiver(post_delete, sender=Movie)
def movie_deleted(sender, instance, **kwargs):
    """电影删除后的信号处理"""
    logger.info(f"电影已删除: {instance.title}")
@receiver(post_save, sender=CrawlLog)
def crawl_log_saved(sender, instance, created, **kwargs):
    """爬虫日志保存后的信号处理"""
    if created:
        logger.info(f"新爬虫日志已创建: {instance.start_time}")
    else:
        logger.info(f"爬虫日志已更新: {instance.start_time}")

from .services import WorldMapDataService
#################################最下面添加即可
@receiver([post_save, post_delete], sender=Movie)
def clear_worldmap_cache(sender, instance, **kwargs):
    """当电影数据更新时清除缓存"""
    WorldMapDataService.clear_cache()
        